<?php

namespace plugin\wechat\app\tenant\controller;

use app\common\constant\Platform;
use app\common\service\App as AppService;
use app\common\service\Tenant as TenantService;
use plugin\wechat\app\TenantController;
use support\Response;
use plugin\wechat\app\model\Mp as MpM;
use plugin\wechat\app\model\Mini as MiniM;
use plugin\wechat\app\model\Bot as BotM;

class Index extends TenantController
{
    public function index()
    {
        return $this->show();
    }

    /**
     * 切换账号
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     * Author: fudaoji<fdj@kuryun.cn>
     */
    public function switch(){
        $id = input('id', 0);
        $type = input('type', Platform::MP);
        switch($type){
            case Platform::MINI:
                $key = CURRENT_MINI;
                $data = MiniM::where([$this->tenantWhere()])->find($id);
                break;
            case Platform::WECHAT:
                $key = CURRENT_WECHAT;
                $data = BotM::where([$this->tenantIdWhere()])->find($id);
                break;
            default:
                $key = CURRENT_MP;
                $data = MpM::where([$this->tenantWhere()])->find($id);
        }
        session([$key => $data]);
        /*if(session('referer_url')){
            $url = session('referer_url');
            session(['referer_url' => null]);
        }else{
            $url = url('welcome', ['type' => $type]);
        }*/
        $url = url('welcome', ['type' => $type]);
        return $this->redirect($url);
    }

    public function welcome(){
        $type = input('type', Platform::MP);
        $where = [
            $this->tenantIdWhere(),
            ['status','=', 1]
        ];
        $assign = [
            'type' => $type,
            'types' => Platform::wechatTypes()
        ];
        switch ($type){
            case Platform::MINI;
                $current_app = session(CURRENT_MINI);
                $data_list = MiniM::where($where)
                    ->order('update_time', 'desc')
                    ->paginate($this->pageSize);
                if(empty($current_app) && count($data_list)){
                    session([CURRENT_MINI => $data_list[0]]);
                    $current_app = session(CURRENT_MINI);
                }
                foreach ($data_list as $k => $v){
                    $v['title'] = $v['nick_name'];
                    $v['subtitle'] = $v['user_name'];
                    $v['cover'] = $v['head_img'];
                    $data_list[$k] = $v;
                }
                break;
            case Platform::WECHAT;
                $current_app = session(CURRENT_WECHAT);
                $data_list = BotM::where($where)
                    ->order('update_time', 'desc')
                    ->paginate($this->pageSize);
                if(empty($current_app) && count($data_list)){
                    session([CURRENT_WECHAT => $data_list[0]]);
                    $current_app = session(CURRENT_WECHAT);
                }
                foreach ($data_list as $k => $v){
                    $v['title'] = $v['nickname'];
                    $v['subtitle'] = $v['uin'];
                    $v['cover'] = $v['headimgurl'];
                    $data_list[$k] = $v;
                }
                break;
            default:
                $current_app = session(CURRENT_MP);
                $data_list = MpM::where($where)
                    ->order('update_time', 'desc')
                    ->paginate($this->pageSize);
                if(empty($current_app) && count($data_list)){
                    session([CURRENT_MP => $data_list[0]]);
                    $current_app = session(CURRENT_MP);
                }
                foreach ($data_list as $k => $v){
                    $v['title'] = $v['nick_name'];
                    $v['subtitle'] = $v['user_name'];
                    $v['cover'] = $v['head_img'];
                    $data_list[$k] = $v;
                }
                break;
        }
        $assign['logo'] = plugin_static('static/imgs/icon64_' . $type . '_logo.png');
        $assign['data_list'] = $data_list;
        $assign['current_app'] = $current_app;
        return $this->show($assign);
    }

    /**
     * 获取初始化数据
     * @return Response
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     * @author: fudaoji<fdj@kuryun.cn>
     */
    public function getSystemInit(){
        $app_info = AppService::getApp(request()->plugin);
        $homeInfo = [
            'title' => '切换账号',
            'href'  => url('index/welcome'),
        ];
        $logoInfo = [
            'title' => $app_info['title'],
            'href' => url('index/index'),
            'image' => $app_info['logo']
        ];

        $menuInfo = \plugin\wechat\app\tenant\service\Auth::getMenuList($this->tenantInfo());
        $systemInit = [
            'homeInfo' => $homeInfo,
            'logoInfo' => $logoInfo,
            'menuInfo' => $menuInfo,
        ];
        return json($systemInit);
    }
}
